package com.jjh.users.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jjh.common.service.BaseService;
import com.jjh.users.pojo.AStatRes;
import com.jjh.users.pojo.AStatResEnum;
import com.jjh.users.service.IUsersDbService;
import com.jjh.users.service.IUsersOperService;

@Service("usersOperService")
public class UsersOperServiceImpl extends BaseService implements IUsersOperService
{
	@Autowired
	private IUsersDbService usersDbService;

	/**
	 * 假如说时间传的是currDate;2020-03-02 11:20:30
	 * 要的是昨天的数据:
	 * 2020-03-01 00:00:00 ====> 2020-03-02 00:00:00
	 */
	@Override
	public JSONObject saveOneStatResService(Date currDate)
	{
		JSONObject resultDbJSON = new JSONObject();
		Map<String,Object> condMap = new HashMap<String,Object>();
		
		/*=====查询指定时间的数据======*/
		Calendar calendar = Calendar.getInstance() ; 
		calendar.setTime(currDate);
		/* 表调成凌晨:00:00:00 */
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		/* 计算时间范围 */
		Date edDate = calendar.getTime() ; 
		calendar.add(Calendar.DATE, -1);
		Date stDate = calendar.getTime() ; 
		
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		/* 这个数据,条件已经限制死了,只能是一条 */
		List<Map<String, Object>> resultList = this.usersDbService.statLogListService(null, condMap);
		Map<String, Object> dataMapTemp = resultList.get(0);
		
		AStatRes statRes = new AStatRes();
		statRes.setCurrDate(stDate);
		/* 它是一个json */
		statRes.setUsersData(JSON.toJSONString(dataMapTemp));
		statRes.setStatus(AStatResEnum.STATUS_ENABLED.getCode());
		/* 保存一条记录 */
		resultDbJSON = this.usersDbService.saveOneStatResService(statRes);
		return resultDbJSON;
	}
}
